﻿Public Class ucApplication1
    Inherits System.Web.UI.UserControl

	Public Event NextStep()
	Public Event Cancel()
	Public Event Alert(ByVal msg As String)

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		If Not Page.IsPostBack Then
			If GlobalApp.RegisterInfo IsNot Nothing Then
				LoadData()
			End If
		End If
		InitJS()

		Me.OFFICER_APP_ID.Attributes.Add("onBlur", "ChangeFormat(this,0,'.','',false,'text');ChangFormatID();")
		'Me.OFFICER_APP_ID.Attributes.Add("onFocus", "ChangFormatID_OnFocus(this);")

		If GlobalApp.CreateAppStep = Common.EnumType.ECreateAppStep.Step4 Then
			Me.btnLast.Visible = True
		End If
	End Sub

	Private Sub InitJS()
		Dim sbJS As New Text.StringBuilder
		sbJS.AppendLine("var brithdayID = '" & Me.OFFICER_BIRTHDATE.ClientID & "';")
		sbJS.AppendLine("var carddayID = '" & Me.OFFICER_CARD_EXPIREDATE.ClientID & "';")
		sbJS.AppendLine("var appID = '" & Me.OFFICER_APP_ID.ClientID & "';")
        sbJS.AppendLine("var cardID = '" & Me.OFFICER_CARD_NO.ClientID & "';")
        sbJS.AppendLine("var cardTypeDesc = '" & Me.OFFICER_CARD_TYPE_DESC.ClientID & "';")
		Me.ltrJS.Text = "<script language=""JavaScript"">" & sbJS.ToString & "</script>"
	End Sub

	Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnNext.Click
		Dim registerInfo As Entity.OfficerInfoItem
		If GlobalApp.RegisterInfo IsNot Nothing Then
			registerInfo = GlobalApp.RegisterInfo
		Else
			registerInfo = New Entity.OfficerInfoItem
		End If

		With registerInfo
			.officer_app_id = Me.OFFICER_APP_ID.Text
			.offcier_app_tel = Me.OFFCIER_APP_TEL.Text
			.officer_firstname_tha = Me.OFFICER_FIRSTNAME_THA.Text
			.officer_lastname_tha = Me.OFFICER_LASTNAME_THA.Text
			.officer_firstname_eng = Me.OFFICER_FIRSTNAME_ENG.Text
			.officer_lastname_eng = Me.OFFICER_LASTNAME_ENG.Text
            .officer_sex = Me.OFFICER_SEX.SelectedValue
            .officer_card_type = Me.ddlCardType.SelectedValue
            If Me.ddlCardType.SelectedValue <> "OTH" Then OFFICER_CARD_TYPE_DESC.Text = ""
            .officer_card_type_desc = Me.OFFICER_CARD_TYPE_DESC.Text.Trim
            .officer_card_no = Me.OFFICER_CARD_NO.Text
            '.officer_birthdate = New Date(Me.OFFICER_BIRTHDATE.Year, Me.OFFICER_BIRTHDATE.Month, Me.OFFICER_BIRTHDATE.Day).AddYears(-543)
            If Common.UtilityDateTime.ConvertDate(Me.OFFICER_BIRTHDATE.Text).HasValue Then
                .officer_birthdate = Common.UtilityDateTime.ConvertDate(Me.OFFICER_BIRTHDATE.Text).Value
            Else
                RaiseEvent Alert("กรุณาระบุวันเกิดตามบัตรให้ถูกรูปแบบ (วว/ดด/ปปปป)")
                Exit Sub
            End If

            Try
                '.officer_card_expiredate = New Date(Me.OFFICER_CARD_EXPIREDATE.Year, Me.OFFICER_CARD_EXPIREDATE.Month, Me.OFFICER_CARD_EXPIREDATE.Day).AddYears(-543)
                If Not String.IsNullOrEmpty(Me.OFFICER_CARD_EXPIREDATE.Text) Then
                    If Common.UtilityDateTime.ConvertDate(Me.OFFICER_CARD_EXPIREDATE.Text).HasValue Then
                        .officer_card_expiredate = Common.UtilityDateTime.ConvertDate(Me.OFFICER_CARD_EXPIREDATE.Text).Value
                    Else
                        RaiseEvent Alert("กรุณาระบุวันบัตรหมดอายุให้ถูกรูปแบบ (วว/ดด/ปปปป)")
                        Exit Sub
                    End If
                End If
            Catch
            End Try
            .officer_card_expiredate_type = Me.OFFICER_CARD_EXPIREDATE_TYPE.SelectedValue
            .officer_married_status = Me.OFFICER_MARRIED_STATUS.SelectedValue
            If GlobalApp.Authen IsNot Nothing Then .create_user_id = GlobalApp.Authen.offcier_id
        End With

		If Common.UtilityDateTime.CalAge(registerInfo.officer_birthdate) < 18 Then
			RaiseEvent Alert("คุณสมบัติของผู้สมัครต้องมีอายุมากกว่า 18 ปี")
			Exit Sub
		End If

		If registerInfo.officer_birthdate >= DateTime.Today Then
			RaiseEvent Alert("ท่านต้องเลือกวันเกิดให้มีค่าน้อยกว่าวันที่ปัจจุบัน")
			Exit Sub
		End If

		If Not String.IsNullOrEmpty(registerInfo.officer_app_id) Then
			If BL.Officer.CheckDupAppID(registerInfo.officer_app_id) Then
				RaiseEvent Alert("ไม่สามารถใช้เลขที่ใบสมัคร/รหัสนักธุรกิจ " & registerInfo.officer_app_id & " ในการสมัครได้ เพราะพบเลขที่ใบสมัครนี้ในระบบแล้ว")
				Exit Sub
			End If
		End If

		If Not String.IsNullOrEmpty(registerInfo.offcier_app_tel) Then
			If BL.Officer.CheckDupAppTel(registerInfo.offcier_app_tel) Then
				RaiseEvent Alert("ไม่สามารถใช้หมายเลขโทรศัพท์ " & registerInfo.offcier_app_tel & " ในการสมัครได้ เพราะพบหมายเลขโทรศัพท์นี้ในระบบแล้ว")
				Exit Sub
			End If
		End If

		If Not String.IsNullOrEmpty(registerInfo.officer_card_no) Then
			If BL.Officer.CheckDupCardNo(registerInfo.officer_card_no) Then
				RaiseEvent Alert("พบหมายเลขบัตรประชาชน " & registerInfo.officer_card_no & " นี้ในการสมัครเป็นนักธุรกิจแล้ว")
				Exit Sub
			End If
		End If

		GlobalApp.RegisterInfo = registerInfo

		RaiseEvent NextStep()
		Response.Redirect("CreateApplication1.aspx", False)
	End Sub

	Private Sub LoadData()

		Dim registerInfo As Entity.OfficerInfoItem
		registerInfo = GlobalApp.RegisterInfo
		With registerInfo
			Me.OFFICER_APP_ID.Text = .officer_app_id
			Me.OFFCIER_APP_TEL.Text = .offcier_app_tel
			Me.OFFICER_FIRSTNAME_THA.Text = .officer_firstname_tha
			Me.OFFICER_LASTNAME_THA.Text = .officer_lastname_tha
			Me.OFFICER_FIRSTNAME_ENG.Text = .officer_firstname_eng
			Me.OFFICER_LASTNAME_ENG.Text = .officer_lastname_eng
            Common.Utility.SelectChoice(Me.OFFICER_SEX, .officer_sex)
            Me.ddlCardType.SelectedValue = .officer_card_type
            Me.OFFICER_CARD_TYPE_DESC.Text = .officer_card_type_desc
			Me.OFFICER_CARD_NO.Text = .officer_card_no
			'Me.OFFICER_BIRTHDATE.value = .officer_birthdate.AddYears(543).ToString("dd/MM/yyyy", Common.Utility.CulInfo)
			Me.OFFICER_BIRTHDATE.Text = .officer_birthdate.Value.AddYears(543).ToString("dd/MM/yyyy", Common.Utility.CulInfo)
			Try
				'Me.OFFICER_CARD_EXPIREDATE.value = .officer_card_expiredate.Value.AddYears(543).ToString("dd/MM/yyyy", Common.Utility.CulInfo)
				Me.OFFICER_CARD_EXPIREDATE.Text = .officer_card_expiredate.Value.AddYears(543).ToString("dd/MM/yyyy", Common.Utility.CulInfo)
			Catch
			End Try
			Common.Utility.SelectChoice(Me.OFFICER_CARD_EXPIREDATE_TYPE, .officer_card_expiredate_type)
			Common.Utility.SelectChoice(Me.OFFICER_MARRIED_STATUS, .officer_married_status)
		End With
	End Sub

	Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCancel.Click
		Me.Response.Redirect("mainpage.aspx", False)
	End Sub

	Protected Sub btnLast_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLast.Click
		Response.Redirect("CreateApplication3.aspx", False)
	End Sub

    Protected Sub ddlCardType_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlCardType.SelectedIndexChanged
        Try
            If ddlCardType.SelectedValue = "OTH" Then
                lblCardType.Text = "เลขที่บัตรอื่นๆ *:"
                OFFICER_CARD_TYPE_DESC.Enabled = True
            Else
                OFFICER_CARD_TYPE_DESC.Text = ""
                If ddlCardType.SelectedValue = "PID" Then lblCardType.Text = "เลขที่บัตรประชาชน *:"
                If ddlCardType.SelectedValue = "CID" Then lblCardType.Text = "เลขที่บัตรนิติบุคคล *:"
                If ddlCardType.SelectedValue = "FID" Then lblCardType.Text = "เลขที่บัตรคนต่างชาติ *:"
                OFFICER_CARD_TYPE_DESC.Text = ""
                OFFICER_CARD_TYPE_DESC.Enabled = False
            End If
        Catch ex As Exception

        End Try
    End Sub

End Class